/*
 * @Author: wuqingchun
 * @Date: 2024-09-23 14:30:20
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-10-03 16:11:26
 */

#ifndef __CHAT_REDIS_SUBPUB_H__
#define __CHAT_REDIS_SUBPUB_H__

#include <thread>

#include "../../deps/hiredis/async.h"
#include "../../deps/hiredis/adapters/libevent.h"

class CChatRedisSubPub
{
public:
    CChatRedisSubPub();
    ~CChatRedisSubPub();

    int32_t Init();
    void Exit();

    int32_t Connect(const char* pcHost, uint16_t usPort);
    int32_t Publish(int iChannel, const char* pcMessage);
    int32_t Publish(const char* pcChannel, const char* pcMessage);
    int32_t Subscribe(int iChannel);
    int32_t Subscribe(const char* pcChannel);

    void SetMessageCallback()
    {
        
    }
    
private:
    static void __OnConnect(const redisAsyncContext* pstRedisCtx, int iStatus);
    static void __OnDisconnect(const redisAsyncContext* pstRedisCtx, int iStatus);
    static void __OnPublishReply(struct redisAsyncContext* pstRedisCtx, void* pReply, void* pPrivate);
    static void __OnSubscribeReply(struct redisAsyncContext* pstRedisCtx, void* pReply, void* pPrivate);

    void __ThreadEventFunc();
private:
    // libevent事件对象
    event_base* m_pstEventBase = nullptr;
    std::thread m_objEventThread;
    
    //  hiredis 异步连接上下文
    redisAsyncContext* m_pstRedisCtx = nullptr;
};


#endif //__CHAT_REDIS_SUBPUB_H__

